package com.txx.design.factory.simplefactory;

/*
 * @description //TODO 简单工厂
 * @exception
 * @author tuxx
 * @date 2019/3/13 16:18
 */
public class ViedoFactory {

    private static ViedoFactory v = null;

    /*public Viedo getViedo(String type){
        if("java".equalsIgnoreCase(type)){
            return new JavaViedo();
        }else if("python".equalsIgnoreCase(type)){
            return new PythonViedo();
        }
        return null;
    }*/

    //通过反射机制来尽量遵守开闭原则减少对工厂类的修改
    public Viedo getViedo(Class c){
        Viedo v = null;
        try {
            Object obj = Class.forName(c.getName()).newInstance();
            if(obj instanceof Viedo){
                v = (Viedo)obj;
            }else{
                System.out.println("传入的Class对象错误...");
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return v;
    }

    private ViedoFactory(){}

    public static ViedoFactory getInstance(){
        if(v == null){
            v = new ViedoFactory();
        }
        return v;
    }
}
